
import {localSave, localRead, getToken, apiResult} from '@/libs/util';
import MapToolsData from "@/view/Home/map/maptools/data/MapToolsData";
import GPS from "@/view/Home/map/maptools/tool/Gps";
import axios from '@/libs/api.request'
import {solveGps0} from "@/view/Home/map/maptools/tool/GpsUtils";

class MapInfo {
  constructor() {
    this.mapToolsData = new MapToolsData();
    this.mapInfo={
      projection:"EPSG:4326",
      center:[114.236262, 22.677236],
      minZoom:2,
      maxZoom:20,
      zoom:14,
      mode:'ONLINE',
      mapName:null,
      mapType:null,
      mapDesc: null,
      coordinateType:'WGS84',
      sub:{id:2,name:'ArcgisYX',desc:'影像',type:"YX",disabled:false,selected:false,divided:false,
        url:[
          'https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}'
        ]
      },
    };
  }

  /** 设置中心 */
/*  setCenter(point){
    const storage = localRead("MAP-INFO");
    if (!!storage) {
      this.mapInfo = JSON.parse(storage);
    }
    this.mapInfo.center = point;
    localSave("MAP-INFO",JSON.stringify(this.mapInfo));
  }
*/
  get(){
    const storage = localRead("MAP-INFO");
    if (!!storage){
      this.mapInfo = JSON.parse(storage);
    }else{
      this.set("GaoDe","GaoDeSL");
    }
    //console.log(">>>>>>>>> MapInfo.get(): this.mapInfo=%o",this.mapInfo);
    return this.mapInfo;
  }

/*  set(mapName,subName){
    //this.get();
    const custMap=this.mapToolsData.getCustomMap(mapName);
    let info={
      projection: this.mapInfo.projection,
      center: this.mapInfo.center,
      minZoom:custMap.minZoom,
      maxZoom:custMap.maxZoom,
      zoom:this.mapInfo.zoom,
      mode:'ONLINE',
      mapName:custMap.name,
      mapType:'',
      mapDesc: custMap.desc,
      coordinateType:custMap.coordinateType
    };

    if (!!subName){
      if (!!custMap.sub){
        for(let i=0;i<custMap.sub.length;i++){
          if (custMap.sub[i].name == subName){
            info.sub=custMap.sub[i];
            info.mapType = custMap.sub[i].name;
            break;
          }
        }
      }
    }else{
      if (!!custMap.sub){
        info.sub=custMap.sub[0];
        info.mapType = custMap.sub[0].name;
      }
    }
    //console.log("info:%o",info);
    this.mapInfo = info;
    //console.log("set:%o",JSON.stringify(this.mapInfo));
    localSave("MAP-INFO",JSON.stringify(this.mapInfo));
  }*/

  maxZoom(){return this.mapInfo.maxZoom;}
  minZoom(){return this.mapInfo.minZoom;}

  //minZoom()=>{return this.mapInfo.minZoom;}

  //本地改变地图，不影响后台
  localSet(mapData){
    if (!mapData){return;}
    //console.log(">>>>>localSet.mapData:%o",JSON.stringify(mapData));
    const storage = JSON.parse(localRead("MAP-INFO"));
    console.log(">>>>>localSet.storage:%o",storage);
    const center = solveGps0(this.mapInfo.coordinateType,mapData.map.coordinateType,storage.center[1],storage.center[0]);
    console.log(">>>>>localSet.center:%o",center);
    //this.mapInfo = JSON.parse(storage);
    //return;
    let info={
      projection: !!mapData.projection?mapData.projection:this.mapInfo.projection,
      center: [center.lon,center.lat],
      minZoom:mapData.map.minZoom,
      maxZoom:mapData.map.maxZoom,
      zoom:storage.zoom,
      mode:storage.mode,
      mapName:mapData.map.name,
      mapType:mapData.name,
      mapDesc:mapData.map.desc + mapData.mapType,
      coordinateType:mapData.map.coordinateType
    };

    mapData.map.sub.forEach(item=>{
      if (item.name === mapData.name){
        info.sub = item;
      }
    });
    //console.log("info:%o",info);
    this.mapInfo = info;
    console.log("MapInfo.set:%o",JSON.stringify(this.mapInfo));
    localSave("MAP-INFO",JSON.stringify(this.mapInfo));
  }
  /**
   * 从后台取得地图配置信息
   */
  setFromApi(){
    axios.request({
      /** //admin/map/getMapInfo/ 变成 请求网址: http://admin/map/getMapInfo/
       * /admin/map/getMapInfo/  才对
       */
      url: '/admin/map/getMapInfo/',
      method: 'get',
      dataType: 'json',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8',
        'token': getToken()
      }
    }).then((res)=>{
      //console.log(">>>>>>> MapInfo.setFromApi: %o",res.data.data);
      apiResult(res.data,false,result=>{
        const tempMap=this.mapToolsData.getCustomMap(res.data.data.name);
        let info={
          projection: this.mapInfo.projection,
          center: [res.data.data.centerLongitude,res.data.data.centerLatitude],
          minZoom:res.data.data.minZoom,
          maxZoom:res.data.data.maxZoom,
          zoom:res.data.data.defaultZoom,
          mode:res.data.data.mode,
          mapName:res.data.data.name,
          mapType:'',
          mapDesc: tempMap.desc,
          coordinateType:res.data.data.coord
        };

        if (!!res.data.data.subName){
          if (!!tempMap.sub){
            for(let i=0;i<tempMap.sub.length;i++){
              if (tempMap.sub[i].name === res.data.data.subName){
                info.sub=tempMap.sub[i];
                info.mapType = tempMap.sub[i].name;
                break;
              }
            }
          }
        }else{
          if (!!tempMap.sub){
            info.sub=tempMap.sub[0];
            info.mapType = tempMap.sub[0].name;
          }
        }
        //console.log("info:%o",info);
        this.mapInfo = info;
        console.log("set:%o",JSON.stringify(this.mapInfo));
        localSave("MAP-INFO",JSON.stringify(this.mapInfo));
      })
      /*if (res.data.code === 0){

      }else{
        console.log("!!!!!! error： %o",res.data.msg)
        this.$Message.error(res.data.msg);
      }*/
    }).catch((err)=>{
      console.log("!!!!!! error： %o",err)
    })
  }
}
export default MapInfo;
